#!/bin/bash

# 执行结果判断函数
function log() {
    if [ $? -eq 0 ]; then
        echo -e "$(date -d "+8 hours"): \e[32m$1==========>succeed\e[0m" >> /var/log/mf-backup.log
    else
        echo -e "$(date -d "+8 hours"): \e[31m$1==========>failed\e[0m" >> /var/log/mf-backup.log
        exit 1
    fi
}

dir=/data/backup/system/
days_old=30

files=$(find "$dir" -type f -name "*.qcow2" -mtime +"$days_old")
if [ -z "$files" ]; then
    exit 0
fi

for file in $files;
do
    rm -rf "$file"
    log "Clean-exprie $file"
done

echo '-------------------------------------------------------------------------------------------------' >> /var/log/mf-backup.log
